﻿namespace OutLook.Module.Infrastructure
{
    using System;
    using OutLook.Module.Infrastructure.Interfaces;

    /// <summary>
    /// Provides basic functionality of domain logic controller
    /// </summary>
    public abstract class DomainControllerBase : IDisposable
    {
        #region Constructors

        protected DomainControllerBase(IRepository context)
        {
            m_Storage = context;
            m_IsDIContext = context is IRepository;
        }

        #endregion

        #region Properties

        public IRepository Storage { get { return m_Storage; } }

        #endregion

        #region IDisposable members

        protected abstract void Dispose(bool disposeManaged);

        public void Dispose()
        {
            Dispose(true);

            ///utilize storage code
            if (m_Storage != null & !m_IsDIContext)
                m_Storage.Dispose();

            GC.SuppressFinalize(this);
        }

        #endregion

        #region Finalizer

        ~DomainControllerBase()
        {
            Dispose(false);
        }

        #endregion

        #region Fields

        private readonly bool m_IsDIContext = false;
        private readonly IRepository m_Storage = null;

        #endregion
    }
}
